package com.kamisama.utopia.constant.map;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WorldMap {

    public static final double NO_THIS_PLACE = -1.0;
    private static Graph<String> WORLD_MAP = new Graph<>(false);
    private static Graph<Location> CENTRAL_CITY_MAP = new Graph<>(false);
    private static HashMap<String, List<FormatWayCost>> CENTRAL_CITY_WAY_COST = new HashMap<>();

    public static final String WORLD = "理想乡";
    public static final String CENTRAL_CITY = "中心城";
    public static final Location PANTHEON = Location.builder().localName("万神殿").build();
    public static final Location RESCUE_STATION = Location.builder().localName("中心城救助站").build();
    public static final Location PIZZA = Location.builder().localName("甄不戳披萨店").build();

    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    static
    class FormatWayCost {
        String name;
        Double cost;
    }

    static {
        CENTRAL_CITY_MAP.addVertex(PANTHEON, 0);
        CENTRAL_CITY_MAP.addVertex(RESCUE_STATION, 0);
        CENTRAL_CITY_MAP.addVertex(PIZZA, 0);
        CENTRAL_CITY_MAP.addEdge(PANTHEON, RESCUE_STATION, 1);
        CENTRAL_CITY_MAP.addEdge(PANTHEON, PIZZA, 2);

        CENTRAL_CITY_MAP.getVertex().values().forEach(loc -> {
            HashMap<Vertex<Location>, Double> localMap = CENTRAL_CITY_MAP.getSmallestDistanceDijkstra2(loc);
            List<FormatWayCost> formatWayCosts = new ArrayList<>();
            for (Map.Entry<Vertex<Location>, Double> entry : localMap.entrySet()) {
                FormatWayCost formatWayCost = new FormatWayCost();
                formatWayCost.setCost(entry.getValue());
                formatWayCost.setName(entry.getKey().getLabel().getLocalName());
                formatWayCosts.add(formatWayCost);
            }
            CENTRAL_CITY_WAY_COST.put(loc.getLabel().getLocalName(), formatWayCosts);
        });
    }

    public static String getFormatMap(String location) {
        String[] strings = StringUtils.delimitedListToStringArray(location, "-");
        if (strings.length == 3) {
            String first = strings[0];
            String second = strings[1];
            String third = strings[2];
            if (first.equals(WORLD)) {
                if (second.equals(CENTRAL_CITY)) {
                    List<FormatWayCost> formatWayCosts = CENTRAL_CITY_WAY_COST.get(third);
                    StringBuilder result = new StringBuilder("区域地图：\n");
                    for (FormatWayCost cost : formatWayCosts) {
                        result.append(cost.getName()).append("    ").append(cost.getCost().toString()).append("\n");
                    }
                    return result.toString();
                }
            }
        }
        return "Error";
    }

    public static Double getMoveCost(String[] strings, String target) {
        if (strings.length == 3) {
            String first = strings[0];
            String second = strings[1];
            String third = strings[2];
            if (first.equals(WORLD)) {
                if (second.equals(CENTRAL_CITY)) {
                    List<FormatWayCost> formatWayCosts = CENTRAL_CITY_WAY_COST.get(third);
                    return formatWayCosts.stream().filter(cost -> cost.getName().equals(target)).map(FormatWayCost::getCost).findFirst().orElse(
                        NO_THIS_PLACE);
                }
            }
        }
        return NO_THIS_PLACE;
    }
}
